var engine;

var onceFlag=true;
// var phaseFlag:String="login";
var currentUser;
var currentChannel;

function startEngine(channel:String)
{
	if (engine==null || engine.status=="idle") 
	{
		mlf.mcmanager.MessageAgent.getMessageDispatcher().addEventListener("onSync", this);
		var nc:NetConnection;
		nc=mlf.mcmanager.MessageAgent.getMessageSlot("mlf", "defaultConnection");
		if (nc!=null)
		{
			engine=mlf.gatekeeper.native.Caller.collabEngine();
			engine.start(nc, "cs_"+channel);
			currentChannel=channel;
			trace("engine.start: cs_"+channel);
		}
	}
}

function stopEngine()
{
	if (engine.status=="process") 
	{
		engine.stop();
		currentUser="";
		// phaseFlag="login";
		mlf.mcmanager.MessageAgent.getMessageDispatcher().removeEventListener("onSync", this);
	}
}

function onSync(evtObj:Object)
{
	 trace("onSync");
	registerTmp();
	
	// if (evtObj.list!=null) trace("list: "+evtObj.list);
	if (evtObj.list!=null) 
	{
		// if (phaseFlag=="login") Packages.modula.Login.prefetchChannelInfo(evtObj.list);
		// else Packages.modula.Item.checkItems(evtObj.list);
		var listInfo=new Object();
		listInfo.type="onEvtListInfo";
		listInfo.target=this;
		listInfo.param=evtObj.list;
		mlf.mcmanager.MessageAgent.getMessageDispatcher().dispatch(listInfo);
	}
	if (evtObj.param!=null)
	{
		for (var i=0; i<evtObj.param.length; i++)
		{
			if (evtObj.param[i].type=="statement")
			{
				initStatement(evtObj.param[i].slot);
			}
			else 
			{
				trace(currentUser+" "+evtObj.param[i].func);
				var target=mlf.mcmanager.PathResolver.extractPath(evtObj.param[i].path);
				if (target!=null) mlf.gatekeeper.native.Caller.callee(target, evtObj.param[i].func, evtObj.param[i].args);
			}
		}
	}
}

function registerTmp()
{
	if (onceFlag) 
	{	
		// trace("register: "+Packages.task.Composition.username);
		register(Packages.task.Composition.username);
		Packages.task.Composition.reportAudioId();
		onceFlag=false;
	}
}

function initStatement(target)
{
	 trace(this._name+".initStatement");
	var stateMessage:Array=new Array();
	 stateMessage.push(Packages.task.Composition.dataProvider());
	 stateMessage.push(Packages.task.Composition.AudioDataProvider());
	if (currentUser=="collab_admin")
	{
		var args=new Array();
		args.push(Packages.task.Composition.audioId);
		stateMessage.push(Packages.system.Collab.wrapInfo(this, "acceptAdmin", args));
	} 
	// var stateMessage:Array=new Array();
	// stateMessage.push("");
	// trace("initStatement.target: "+target);
	engine.update(stateMessage, target);
}

function acceptAdmin(audioId)
{
	Packages.system.Platform.collabAdminMode=audioId;
}

function updateInfo(ins, func, args)
{
	var actionMessage=wrapInfo(ins, func, args);
	engine.update(actionMessage);
}

function wrapInfo(ins, func, args):Object
{
	var mess=new Object();
	mess.path=mlf.mcmanager.PathResolver.drawPath(ins);
	mess.func=func;
	mess.args=args;
	return mess;
}

function register(slotName:String)
{
	 trace(this._name+".register: "+slotName);
	engine.register(slotName);
	currentUser=slotName;
	// phaseFlag="communication";
}